<template>
	<view class="container b-f"><diy-component :diyItems="items"></diy-component></view>
</template>

<script>
import diyComponent from '@/components/diy/index.vue';
import Tools from '@/utils/Tools.js';
export default {
	components: {
		diyComponent
	},
	data() {
		return {
			page_id: 0,
			items: {},
			page: { style: {} }
		};
	},
	onLoad(options) {
		this.page_id = options.page_id;
		// 加载页面数据
		this.getPageData();
	},
	/**
	 * 下拉刷新
	 */
	onPullDownRefresh() {
		// 获取首页数据
		this.getPageData(() => {
			uni.stopPullDownRefresh();
		});
	},
	/**
	 * 分享当前页面
	 */
	onShareAppMessage: function() {
		// 构建页面参数
		let params = Tools.getShareUrlParams({
			page_id: this.page_id
		});
		return {
			title: this.page.params.share_title,
			path: '/pages/custom/index?' + params
		};
	},
	methods: {
		setShare() {
			// #ifdef H5
			this.$wechat.updateAppMessageShareData({
				title: this.page.params ? this.page.params.share_title : '', // 分享标题
				desc: '推荐你', // 分享描述
				link: Tools.getShareUrl(Tools.getPagePath(), { page_id: this.page_id }), // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: location.origin + '/static/logo/logo.png', // 分享图标
			});
			this.$wechat.onMenuShareTimeline({
				title: this.page.params ? this.page.params.share_title : '', // 分享标题
				link: Tools.getShareUrl(Tools.getPagePath(), { page_id: this.page_id }), // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: location.origin + '/static/logo/logo.png', // 分享图标
			});
			// #endif
		},
		/**
		 * 加载页面数据
		 */
		getPageData(callback) {
			this.$get(this.$api.getPage, { page_id: this.page_id }, res => {
				this.setPageBar(res.data.page);
				this.items = res.data.items;
				this.page = res.data.page;
				this.$nextTick(() => {
					this.$wechat.initJssdk(this.setShare());
					typeof callback === 'function' && callback();
				});
			});
		},
		/**
		 * 设置顶部导航栏
		 */
		setPageBar(page) {
			// 设置页面标题
			uni.setNavigationBarTitle({
				title: page.params.title
			});
			// 设置navbar标题、颜色
			uni.setNavigationBarColor({
				frontColor: page.style.titleTextColor === 'white' ? '#ffffff' : '#000000',
				backgroundColor: page.style.titleBackgroundColor
			});
		}
	}
};
</script>

<style></style>
